package com.yanqu.road.server.cmd.activity.washe.cross;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;
import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.season.SeasonRewardMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_WASHE_GET_RANK_RECORD_FROM_CROSS, desc = "")
public class WasheGetRankRecordFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        int seasonId = washeConfig.getSeasonId();
        String rangeKey = SeasonHelper.getWasheRangeKey(washeConfig.getActivityInfo().getParam6());
        WasheProto.CrossWasheGetRankRecordRespMsg respMsg = WasheProto.CrossWasheGetRankRecordRespMsg.parseFrom(yanQuMessage.getBytes());

        int rankType = respMsg.getRankType();

        ConcurrentHashMap<Long, Integer> userRankMap = new ConcurrentHashMap<>();
        for (int i = 0; i < respMsg.getUserIdCount(); i++) {
            long userId = respMsg.getUserId(i);
            int rank = respMsg.getRank(i);
            userRankMap.put(userId, rank);
        }

        XSeasonUserRank topRank = null;
        if (respMsg.hasTopRank()) {
            XRankProto.XUserRankMsg rankMsg = respMsg.getTopRank();
            topRank = XRankPb.parseXSeasonUserRank(eSystemId.WasheActivity.getValue(), seasonId, rankType, rangeKey, rankMsg);

            // 不管他领没领，都展示榜单第一名的勋章
            Map<Integer, SeasonRewardConfig> configMap = SeasonRewardMgr.getSeasonRewardConfigMap(eSystemId.WasheActivity.getValue(), topRank.getSeasonId(), rankType);
            for (SeasonRewardConfig rewardConfig : configMap.values()) {
                if (rewardConfig.getParamList().get(0) <= 1 && 1 <= rewardConfig.getParamList().get(1)) { // 第一名奖励
                    Property property = PropertyHelper.parseStringToProperty(rewardConfig.getReward());
                    for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
                        if (GoodsMgr.isBadgeUnlockItem(entry.getKey())) { // 勋章道具
                            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(entry.getKey());
                            if (goodsInfo != null) {
                                int badgeId = goodsInfo.getParamList().get(1).intValue();
                                topRank.setParam(String.valueOf(badgeId));
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }

        WasheMgr.addHistoryRank(seasonId, rankType, userRankMap, topRank);
    }

}
